package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import com.fywsy.fyweb.utils.TextUtils;
import org.springframework.stereotype.Component;

/**
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(9)
public class YuanshenGameStrategy extends BaseGameStrategy {

    public static final String URL = "http://113.46.230.156:32221";
    public static final String PAY = URL + "/api?uid=%s&msg=%s+%s&cmd=1116";
    public static final String MAIL = URL + "/api?uid=%s&msg=item+add+%s+%s&cmd=1116";
    public static final String  GN = URL + "/api?uid=%s&msg=%s&cmd=1116";

    /**
     * 清理圣遗物
     */
    public static final String CLEAR_SYW = URL + "/api?uid=%s&msg=item+clear+relic&cmd=1116&region=dev_gio";

    /**
     * 清理背包
     */
    public static final String CLEAR_BAG = URL + "/api?uid=%s&msg=item+clear&cmd=1116&region=dev_gio";

    @GongnengMethod(901)
    public String pay(SendItem item) {
        String msg = RestTemplateUtil.sendGetAndGetJsonKey(String.format(PAY, item.uid, item.itemId,item.number), "msg");
        return parseRespMsg(msg);
    }

    @GongnengMethod(902)
    public String mail(SendItem item) {
        String msg = RestTemplateUtil.sendGetAndGetJsonKey(String.format(MAIL, item.uid, item.itemId,item.number), "msg");
        return parseRespMsg(msg);
    }

    @GongnengMethod(903)
    public String gongneng(SendItem item) {
        String msg = RestTemplateUtil.sendGetAndGetJsonKey(String.format(GN, item.uid, item.itemId), "msg");
        return parseRespMsg(msg);
    }

    /**
     * 五星圣遗物
     * @param item
     * @return
     */
    @GongnengMethod(904)
    public String syw(SendItem item) {
        return mail(item);
    }

    @GongnengMethod(905)
    public String clear(SendItem item) {
        if("1".equals(item.itemId)) {
            String msg = RestTemplateUtil.sendGetAndGetJsonKey(String.format(CLEAR_BAG, item.uid), "msg");
            return parseRespMsg(msg);
        } else if("2".equals(item.itemId)) {
            String msg = RestTemplateUtil.sendGetAndGetJsonKey(String.format(CLEAR_SYW, item.uid), "msg");
            return parseRespMsg(msg);
        }
        return "非法请求";
    }


    private String parseRespMsg(String resp) {
        if(TextUtils.isEmpty(resp)) return "未知错误";
        if (resp.contains("succ")) return "成功";
        if (resp.contains("timeout")) return "连接超时";
        return resp;
    }

    public static void main(String[] args) {
        SendItem item = new SendItem();
        item.uid = "202540100";
        item.itemId = "1171";
        item.number = "1";
        String pay = new YuanshenGameStrategy().mail(item);
        System.out.println(pay);
    }
}

